var model = "";//类型
var state = "";//状态
var work = "";//工作方式
var conVoltage = ""//控制器电压
var conModel = "";//控制器模式
var proName = $(".proName").text();
var conId = sessionStorage.getItem("conId");
var token = localStorage.getItem("key");
var map = {};
map.token = token;
map.controllerId = conId; 
map.pageNum = 1;
map.pageSize = 20;
var request = JSON.stringify(map);
queryCon();
queryLightDetails();
$(function(){
	    showMunu();
		hideImg2();
		showImg2();
				function showMunu(){
					$("#condition_1").click(function(){
						$("#downwindows_1").show();
						$("#downwindows_2").hide();
						$("#downwindows_3").hide();
					});
					$("#condition_2").click(function(){
						$("#downwindows_1").hide();
						$("#downwindows_2").show();
						$("#downwindows_3").hide();
					});
					$("#condition_3").click(function(){
						$("#downwindows_1").hide();
						$("#downwindows_2").hide();
						$("#downwindows_3").show();
					});
					$("#conditionList_1").children().click(function(){
					   //alert($(this).text());		
					   $("#condition_1").text($(this).text());					  
					   $("#downwindows_1").hide();
					   showChoose_1($(this).text());
					   model = $(this).text();
					   queryLightByCondition();
					});
					$("#conditionList_2").children().on("click",function(){
					   //alert($(this).text());
					  
					   $("#condition_2").text($(this).text())
					   $("#downwindows_2").hide();
					   showChoose_2($(this).text());
					    state = $(this).text();
					    queryLightByCondition();
					});
					$("#conditionList_3").children().on("click",function(){
					   //alert($(this).text());
					  
					   $("#condition_3").text($(this).text())
					    $("#downwindows_3").hide();
					    showChoose_3($(this).text()); 
					     work = $(this).text();
					     queryLightByCondition();
					});
				}
				
				function showChoose_1(data){	
					var arr = ['全部','低光强A型','低光强B型','中光强A型','中光强B型','中光强C型','高光强A型','高光强B型'];
					var index = -1;					
					//清空标签上的属性图片
					   $("#conditionList_1").find('p').attr('style','color: ;').find('span').remove();
								
					for(var i = 0;i<arr.length;i++){
						if(arr[i] == data){
							index += i + 1;
						}
					}
		         	$("#conditionList_1").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
				}
				function showChoose_2(data){
					var arr = ['全部','正常','故障'];
						var index = -1;					
					//清空标签上的属性图片
					   $("#conditionList_2").find('p').attr('style','').find('span').remove();
									
					for(var i = 0;i<arr.length;i++){
						if(arr[i] == data){
							index += i + 1;
						}
					}
		         	$("#conditionList_2").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
				}
				function showChoose_3(data){
					var arr = ['全部','恒光','闪光'];
						var index = -1;					
					//清空标签上的属性图片
					   $("#conditionList_3").find('p').attr('style','').find('span').remove();
									
					for(var i = 0;i<arr.length;i++){
						if(arr[i] == data){
							index += i + 1;
						}
					}
		         	$("#conditionList_3").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
				}
				function showImg2(){
					$("#showImg2").click(function(){
						 $(".pic_atlas_con").show();
		                 $("#hideImg2").show();
		                 $("#showImg2").hide();
					});
				}
				function hideImg2() {
	              $("#hideImg2").click(function() {
		          $(".pic_atlas_con").hide();
		          $("#hideImg2").hide();
		          $("#showImg2").show();
	               });
                }
				$("#flash").click(function(){
					location.reload();
				});
});


/**
 * 控制器详情
 */
function queryCon(){
	$.ajax({
	type:"post",
	url:url_prefix+"home/queryCon",
	async:true,
	dataType:"JSON",
	data:{
		request:request
	},
	success:function(data){
		console.log(data);
		if(data.code == '200'){
			var voltage = data.result.voltage;
			var batteryCapacity = data.result.batteryCapacity;
			if(voltage == null || voltage == ""){
				voltage = 0;
			}
			if(batteryCapacity == null || batteryCapacity ==""){
				batteryCapacity = 0;
			}
			if(batteryCapacity == 0){
				voltage = "AC"+voltage+"v"
				$("#voltage").text(voltage);
				$("#battey").hide()
			}else{
				voltage = "DC"+voltage+"v"
				$("#voltage").text(voltage);
				$("#battey").show()
			}
			$("#batteryCapacity").text(batteryCapacity+"%");
			
			conVoltage = voltage;
			if(data.result.lightState == '白天'||data.result.lightState == '黎明'){
				$("#lightState").attr('src','img/state_icon_day@2x.png')
			}else{
				$("#lightState").attr('src','img/state_icon_night@2x.png')
			}
			$("#controllerState").text(data.result.controllerState);
			var position = data.result.position;
			if(position == "null/null"){
				position = "0/0";
			}
			$("#position").text("经度/纬度 : "+position);
			
		}else if(data.code == '300'){
			    mui.toast("账号在其他设备上登录")
			    window.location.href = "fastLogin.html";
		}
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {

			mui.toast("连接服务器失败,请稍后重试``````");
		}
	
});
}
/**
 * 灯具详情
 */
function queryLightDetails(){
	$.ajax({
		type:"post",
		url:url_prefix+"home/queryLightDetails",
		dataType:"JSON",
		data:{
			request:request
		},
		success:function(data){
				console.log(data);
			if(data.code == '200'){
				$("#conList").empty();
				data.result.pageSize;//每页条数
				data.result.pageNum;//第几页
				var arr = data.result.list//分页数据
				$("#total").text(data.result.total);
				var yes = 0;
				var no = 0;
				for(var i = 0;i<arr.length;i++){
					var str = "";
					str += "<div class='conList'><P class='float_l conList_nub'>"+arr[i].lightId+"</P>"+
					"<div class='conList_top float_r'>";
						if(arr[i].lightWork == '恒光'){
					       str += "<img class='conList_state_icon float_l' src='img/icon_lihgt.png'>";
				        }else if (arr[i].lightWork == '闪光'){
					       str += "<img class='conList_state_icon float_l' src='img/xmxq_zc_sg.png'>";
				        }
				    str += "<P class='conList_voltage float_l'>"+arr[i].lightWork+"</P>"+
				    "<div class='clear'></div>"+
				    "</div>"+
				    "<div class='clear'></div>"+
				    "<p class='float_l conList_det'>"+arr[i].lightType+"</p>"+
				    "<p class='float_r conList_state'>"+arr[i].lightState+"</p>"+
				    "<div class='clear'></div>";	
					if(arr[i].lightWork == '闪光'){
					str+="<div class='light_del'>"+
						"<p class='float_l'>闪光频率"+arr[i].lightingFrequency+"<p>"+
						"<p class='light_del_fen float_l'>闪光起始点"+arr[i].startPoint+"</p>"+
						"<p class='float_l'>闪光持续时间"+arr[i].flashDura+"</p>"+
						"<div class='clear'></div>"+
						"</div>";
					}
				    str+= "</div></div>";
					
				    $("#conList").append(str);
				}
				$("#yesorno").text(data.result.LightInfo.badLight+"/"+data.result.LightInfo.goodLight);
				
			}else if(data.code == '300'){
				 mui.toast("账号在其他设备上登录")
			    window.location.href = "fastLogin.html";
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {

			mui.toast("连接服务器失败,请稍后重试``````");
		}
	});
}
/**
 * 列表筛选
 */
function queryLightByCondition(){
var lightWork = work;
var lightState = state;
var lightType = model;
	var map = {};
map.token = token;
//map.controllerId = conId; 
map.controllerId = 1; 
map.pageNum = 1;
map.pageSize = 20;
map.lightWork = lightWork;
map.lightState = lightState;
map.lightType = lightType;
var request = JSON.stringify(map);
$.ajax({
	url:url_prefix + "home/queryLightByCondition",
	type:"POST",
	dataType:"JSON",
	data:{
		request:request
	},
	success:function(data){
		console.log(data);
		if(data.code == '200'){
			$("#conList").empty();
			 data.result.pageSize;//每页条数
				data.result.pageNum;//第几页
				var arr = data.result.list//分页数据
				$("#total").text(data.result.total);
				var yes = 0;
				var no = 0;
				for(var i = 0;i<arr.length;i++){
					if(arr[i].lightState == '正常'||arr[i].lightState == '未连接'){
						yes++;
					}else if(arr[i].lightState == '故障'){
						no++;
					}
					var str = "";
					str += "<div class='conList'><P class='float_l conList_nub'>"+arr[i].lightId+"</P>"+
					"<div class='conList_top float_r'>";
						if(arr[i].lightState == '白天' || arr[i].lightState == '黎明'){
					       str += "<img class='conList_state_icon float_l' src='img/state_icon_day@2x.png'>";
				        }else{
					       str += "<img class='conList_state_icon float_l' src='img/state_icon_night@2x.png'>";
				        }
				    str += "<P class='conList_voltage float_l'>"+conVoltage+"</P>"+
				    "<div class='clear'></div>"+
				    "</div>"+
				    "<div class='clear'></div>"+
				    "<p class='float_l conList_det'>"+arr[i].lightType+"</p>"+
				    "<p class='float_r conList_state'>"+arr[i].lightState+"</p>"+
				    "<div class='clear'></div>"+
				    "<div class='light_del'>"+
				    "<p class='float_l'>闪光频率"+arr[i].lightingFrequency+"<p>"+
				    "<p class='light_del_fen float_l'>闪光起始点"+arr[i].startPoint+"</p>"+
				    "<p class='float_l'>闪光持续时间"+arr[i].flashDura+"</p>"+
				    "<div class='clear'></div>"+
				    "</div></div></div>";
				    $("#conList").append(str);
				}
				$("#yesorno").text(yes+"/"+no);
				
		}else if(data.code == '300'){
				 mui.toast("账号在其他设备上登录")
			    window.location.href = "fastLogin.html";
		}
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {

			mui.toast("连接服务器失败,请稍后重试``````");
		}
});
}
